import {Component, OnInit} from '@angular/core';
import {DraIntegralLottery, DraIntegralLotteryCondition} from '../../dra-integral-lottery.type';
import {ActivatedRoute, Router} from '@angular/router';
import {Services} from '../../../../../frame/module/services';
import {ErrRes} from '../../../../../frame/global/err-res';
import {isNullOrUndefined} from 'util';
import {UrlRou} from '../../../../../frame';
import {ModifyDraIntegralLotteryComponent} from '../modify-dra-integral-lottery.component';
import {Zero3Alert} from '../../../../../frame/global/zero3-alert/zero3-alert.component';
import {GooBrandCondition} from '../../../../goods-management/goo-brand/goo-brand.type';
import * as fa from '@fortawesome/free-solid-svg-icons';
import {FroalaEditor} from '../../../../../frame/global/froala-editor';
import {Zero3Enum} from '../../../../../frame/enum/zero3-enum';

@Component({
    selector: 'app-main-modify-dra-integral-lottery',
    templateUrl: './main-modify-dra-integral-lottery.component.html',
    styleUrls: ['./main-modify-dra-integral-lottery.component.css']
})
export class MainModifyDraIntegralLotteryComponent implements OnInit {

    froalaEditorOptions = Object.assign(FroalaEditor.options, {});

    errRes = new ErrRes();

    domain: DraIntegralLottery = new DraIntegralLottery();

    extend = {
        domain: {
            serGoodsDrawNumber: {
                items: [] // {id: '10001', name: '10001'}, {id: '10002', name: '10002'}, {id: '10007', name: '10007'}
            },
            serGooBrand: {
                items: [],
                icon: fa.faSearchPlus,
                nickname: '专区类型',
                change: (event) => {
                    console.log(event);
                }
            },
            default: () => {
                this.domain.serAudit = 'WAIT_SUBMIT';
                this.domain.serUsing = 'ENABLE';
            },
            serAudit: () => {
                return 'serAudit';
            },
            serGoodsDrawStartDate: {
                icon: fa.faCalendar,
                nickname: '活动开始时间',
                value: () => {
                    // return `${this.condition.createTimeOUTSET} - ${this.condition.createTimeFINISH}`;
                    return this.domain.serGoodsDrawStartDate;
                },
                focus: (event) => {
                    this.domain.serGoodsDrawStartDate = event.target.value;
                },
                blur: (event) => {
                }
            },
            serGoodsDrawEndDate: {
                icon: fa.faCalendar,
                nickname: '活动结束时间',
                value: () => {
                    // return `${this.condition.createTimeOUTSET} - ${this.condition.createTimeFINISH}`;
                    return this.domain.serGoodsDrawEndDate;
                },
                focus: (event) => {
                    this.domain.serGoodsDrawEndDate = event.target.value;
                },
                blur: (event) => {
                }
            },
            serGoodsDrawOpenDate: {
                icon: fa.faCalendar,
                nickname: '活动开奖时间',
                value: () => {
                    // return `${this.condition.createTimeOUTSET} - ${this.condition.createTimeFINISH}`;
                    return this.domain.serGoodsDrawOpenDate;
                },
                focus: (event) => {
                    this.domain.serGoodsDrawOpenDate = event.target.value;
                },
                blur: (event) => {
                }
            },
            fileGoodsDrawP2: {
                oss: Services.aliOSS.simple(),
                change: (event) => {
                    console.log(event.target.value); // file name
                    this.extend.domain.fileGoodsDrawP2.oss.uploadFile((response, status, headers) => {
                        if (status === 200) {
                            this.extend.domain.fileGoodsDrawP2.oss.fileUrl = JSON.parse(response).detail.fileUrl;
                            this.domain.serGoodsDrawP2 = this.extend.domain.fileGoodsDrawP2.oss.fileUrl;
                        } else {
                            this.extend.domain.fileGoodsDrawP2.oss.fileUrl = '';
                            this.domain.serGoodsDrawP2 = this.extend.domain.fileGoodsDrawP2.oss.fileUrl;
                            Zero3Alert.req('fail');
                        }
                    });
                }
            },
            fileGoodsDrawP3: {
                oss: Services.aliOSS.simple(),
                change: (event) => {
                    console.log(event.target.value); // file name
                    this.extend.domain.fileGoodsDrawP3.oss.uploadFile((response, status, headers) => {
                        if (status === 200) {
                            this.extend.domain.fileGoodsDrawP3.oss.fileUrl = JSON.parse(response).detail.fileUrl;
                            this.domain.serGoodsDrawP3 = this.extend.domain.fileGoodsDrawP3.oss.fileUrl;
                        } else {
                            this.extend.domain.fileGoodsDrawP3.oss.fileUrl = '';
                            this.domain.serGoodsDrawP3 = this.extend.domain.fileGoodsDrawP3.oss.fileUrl;
                            Zero3Alert.req('fail');
                        }
                    });
                }
            },
            fileGoodsDrawP4: {
                oss: Services.aliOSS.simple(),
                change: (event) => {
                    console.log(event.target.value); // file name
                    this.extend.domain.fileGoodsDrawP4.oss.uploadFile((response, status, headers) => {
                        if (status === 200) {
                            this.extend.domain.fileGoodsDrawP4.oss.fileUrl = JSON.parse(response).detail.fileUrl;
                            this.domain.serGoodsDrawP4 = this.extend.domain.fileGoodsDrawP4.oss.fileUrl;
                        } else {
                            this.extend.domain.fileGoodsDrawP4.oss.fileUrl = '';
                            this.domain.serGoodsDrawP4 = this.extend.domain.fileGoodsDrawP4.oss.fileUrl;
                            Zero3Alert.req('fail');
                        }
                    });
                }
            },
            fileGoodsDrawView: {
                oss: Services.aliOSS.simple(),
                change: (event) => {
                    console.log(event.target.value); // file name
                    this.extend.domain.fileGoodsDrawView.oss.uploadFile((response, status, headers) => {
                        if (status === 200) {
                            this.extend.domain.fileGoodsDrawView.oss.fileUrl = JSON.parse(response).detail.fileUrl;
                            this.domain.serGoodsDrawView = this.extend.domain.fileGoodsDrawView.oss.fileUrl;
                        } else {
                            this.extend.domain.fileGoodsDrawView.oss.fileUrl = '';
                            this.domain.serGoodsDrawView = this.extend.domain.fileGoodsDrawView.oss.fileUrl;
                            Zero3Alert.req('fail');
                        }
                    });
                }
            },
            files: {
                fileUrls: [],
                oss: Services.aliOSS.simple(),
                change: (event) => {
                    console.log(event.target.value); // file name
                    this.extend.domain.files.oss.uploadFile((response, status, headers) => {
                        if (status === 200) {
                            this.extend.domain.files.fileUrls.push(JSON.parse(response).detail.fileUrl);
                            this.extend.domain.files.oss.fileUrl = JSON.parse(response).detail.fileUrl;
                            this.domain.serGoodsDrawP1 = this.extend.domain.files.fileUrls.toString();
                        } else {
                            this.extend.domain.files.oss.fileUrl = '';
                            this.domain.serGoodsDrawP1 = this.extend.domain.files.fileUrls.toString();
                            Zero3Alert.req('fail');
                        }
                    });
                },
                del: {
                    click: (event, fileUrl) => {
                        const urlIndex = this.extend.domain.files.fileUrls.findIndex(item => item === fileUrl);
                        this.extend.domain.files.fileUrls.splice(urlIndex, 1);
                        this.domain.serGoodsDrawP1 = this.extend.domain.files.fileUrls.toString();
                    }
                }
            },
        },
    };
    func = {
        loadDomain: (uuid: string) => {
            const condition: DraIntegralLotteryCondition = new DraIntegralLotteryCondition();
            condition.uuid = uuid;
            Services.draIntegralLottery.info(condition).subscribe(item => {
                if (!isNullOrUndefined(item) && !isNullOrUndefined(item.domain)) {
                    this.domain = item.domain;
                    if (!isNullOrUndefined(this.domain.serGoodsDrawP1)) {
                        this.extend.domain.files.fileUrls = this.domain.serGoodsDrawP1.split(',');
                    }
                    // init extend.domain.serGoodsDrawNumber.items
                    this.extend.domain.serGoodsDrawNumber.items = [];
                    let drawNumberItemsCount = 0;
                    if (Zero3Enum.DRAW_SHIRENTUAN === this.domain.serGoodsDrawPeople) {
                        drawNumberItemsCount = 10;
                    } else if (Zero3Enum.DRAW_BAIRENTUAN === this.domain.serGoodsDrawPeople) {
                        drawNumberItemsCount = 100;
                    } else if (Zero3Enum.DRAW_QIANRENTUAN === this.domain.serGoodsDrawPeople) {
                        drawNumberItemsCount = 1000;
                    }
                    // arr = [...Array(10).keys()]
                    for (let i = 0; i < drawNumberItemsCount; i++) {
                        const dNumber: string = i < 10 ? `1000${i}` :
                            i < 100 ? `100${i}` :
                                i < 1000 ? `10${i}` :
                                    `1000`;
                        console.log(dNumber);
                        this.extend.domain.serGoodsDrawNumber.items.push(
                            {id: dNumber, name: dNumber}
                        );
                    }
                }
            });
            Services.gooBrand.list(new GooBrandCondition(), {
                page: 0,
                size: 999,
                orders: 'createTime-desc,serNum-asc'
            }).subscribe(item => {
                if (!isNullOrUndefined(item) && item.code === 'success') {
                    this.extend.domain.serGooBrand.items = [];
                    item.page.content.forEach(gooBrand => {
                        this.extend.domain.serGooBrand.items.push({id: gooBrand.uuid, name: gooBrand.serBarndName});
                    });
                }
            });
        },
        update: {
            req: (event) => {
                Services.draIntegralLottery.update(this.domain, this.func.update.callback).subscribe();
            },
            callback: {
                success: (result) => {
                    this.router.navigate([UrlRou.draIntegralLottery.list]);
                },
                error: (result) => {
                    this.errRes.error = result.error;
                    this.errRes.statusText = result.statusText;
                }
            }
        }
    };

    constructor(public modifyDraIntegralLottery: ModifyDraIntegralLotteryComponent, private router: Router, private route: ActivatedRoute) {
    }

    ngOnInit() {
        this.extend.domain.default();
        this.modifyDraIntegralLottery.main = this;
        const uuid = this.route.snapshot.paramMap.get('uuid');
        this.func.loadDomain(uuid);
    }

}
